home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / powertool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-10  |  17.2 KB  |  601 lines

  1. /*
  2. %    POWERTOOL . C
  3. %
  4. %    Multi-Function Tool for Image Development
  5. %
  6. %    Copyright (c)    Jin Guojun
  7. */
  8.  
  9. #include <math.h>
  10. #include "header.def"
  11. #include "imagedef.h"
  12.  
  13. #ifndef    MaxWinWidth
  14. # define    BufSize    2048L
  15. #else
  16. # define    BufSize    MaxWinWidth
  17. #endif
  18.  
  19. #define    FILLER    10
  20.  
  21. char    TYPE_INFO[] = "input image format for non HIPS image. Default is BYTE";
  22. arg_fmt_list_string    arg_fmt[] =    {
  23.     {"-TIFF", "%N", TiFF, 1, 0,
  24.         "-image_type    TYPE in piping processes."},
  25. /*    {"-[S][I][F[#]]", "%b", Flase, 1, 0, TYPE_INFO},    */
  26.     {"-F[#]", "%N %d", IFMT_FLOAT, 1, 0,
  27.         "Float [force input # frames for non-HIPS color image]"},
  28.     {"-H", "%~ %d %d %d", 0, 4, 0,
  29.         "\n\t[\"]row [column [frame]\"]    specify pure image input size",
  30.         1},
  31.     {"-I", "%N", IFMT_LONG, 1, 0, TYPE_INFO},
  32.     {"-IL", "%N", IFMT_ILC, 1, 0, "input RGB"},
  33.     {"-S", "%N", IFMT_SHORT, 1, 0, (char*)TYPE_INFO},
  34.     {"-P", "%b", False, 1, 0,
  35.         "input not be Piped. It used for less memroy system"},
  36.     {"-W", "%i", BufSize, 1, 1, "maximum width of output [%.0f]"},
  37.     {"-a", "%b", True, 1, 0, "adjusting for factor not equal to 2^x\n\
  38.         or generating HISTO data for gnuplot"},
  39.     {"-f", "%d", True, 1, 0,
  40.         "\tfilling the rest area when starting position is not 1"},
  41.     {"-c", "%d", 1, 1, 1, "\tcolumn enlarge factor"},
  42.     {"-r", "%d", 1, 1, 1, "\trow enlarge factor"},
  43.     {"+", "%~ %d", 2, 2, 1, "\tenlarge factor for both directions"},
  44.     {"-g", "%b", True, 1, 0, "gray scale only"},
  45.     {"-o", "%s", NULL, 1, 1, "output file name"},
  46.     {"-p", "%~ %d %d %d", 1, 4, 1,
  47.         "[\"]row [column [frame]\"]    start position", 1},
  48.     {"-s", "%# %d %d %d", 1, 4, 1,
  49.         "[\"]row [column [frame]\"]    output window", 1},
  50.     {"-slid", "%b", True, 1, 0, "message viewed in one line"},
  51. #ifdef    _DEBUG_
  52.     {"-d", "%b", True, 1, 0, "debug on"},
  53. #endif
  54. #ifdef    FITS_IMAGE
  55.     {"-u", "%N", 'u', 1, 0, "unix FITS type"},
  56. #endif
  57.     {"-v", "%b", True, 1, 0, "verbose"},
  58.     {"-w", "%b", False, 1, 0, "output pure image without header"},
  59.     {"-x[x][X]", "%E %d %x %X", 0, 2, 1,
  60.         "starting offset. skip first # [HEX] bytes.\n"},
  61.     {"    [<] in_file_name [> [-o] out_file_name]\n\n\
  62.  Notes:\n\
  63. %    The regular mode is to allocate large memory for entire input data.\n\
  64. %    The purpose is to speed up and able to pipe in for spreading    \n\
  65. %    multi_frame images.\n\
  66. %    \n\
  67. %    The -P option -- file seek -- is a non piping input mode.    \n\
  68. %    It requires no working memory. So it may be good for smaller    \n\
  69. %    memory system or used in some special case.    \n\
  70. %    \n\
  71. %    The regular mode runs as twice fast as file seek mode for large    \n\
  72. %    data file and about as four time fast as file seek mode for smaller\n\
  73. %    data file in local file server.    \n\
  74. %    If run on remote file server, for smaller data, pipe mode runs as\n\
  75. %    twice fast as file seek mode. For large data, the difference is slight\n\
  76. %    The file seek mode has 98% CPU utilization and memory mode has 49% CPU\n\
  77. %    utilization. The memory mode is 17% - 30% faster than file mode.\n\
  78. %    \n\
  79. %    Input position is start from 1 rather than 0.    \n", "0", 0, 0, 0,
  80.     "End of Notes"},    NULL    };
  81. /*
  82. %    When output columns > input columns, it must be integer times of input
  83. %    columns.
  84. %    This design is for cross network file server by a line writer. For local
  85. %    file server, store output in a buffer and write to disk in once is fast.
  86. %
  87. % AUTHOR:    Jin Guojun - LBL    OCT. 1, 1990
  88. */
  89.  
  90. U_IMAGE    uimg;
  91.  
  92. #define    inbuf    uimg.src
  93. #define    obuf    uimg.dest
  94. #define    pform    uimg.in_form
  95. #define    showin_info    message("input window = %d %d    ", i_row, i_cln);
  96.  
  97. bool    szchange, relocat, pipe=1, gray_scale, init_type=IMAGE_INIT_TYPE,
  98.     forceF, graph=1, header=1, orw, filling, verbose, sliding;
  99. char    i_name[96]={NULL}, *topbd, cmd[128];
  100. MType    Fpos, in_size, o_size, wlen, offs, wk_sz, pxlog_bytes,
  101.     i_cln, i_row, frames=1,    /*    input file window size    */
  102.     o_cln, o_row, o_frm;    /* output file window size. init=0    */
  103. int    frm=1, bgn_cln=1, bgn_row=1,    /*    starting position    */
  104.     enlg_cln, enlg_row,        /*    enlarge factors        */
  105.     mulf_rowofs,    /*    mul_frame end of row offset    */
  106.     ratio, adj,    /*    deviation adjustment        */
  107.     bufsize;
  108.  
  109.  
  110.  
  111. main(argc, argv)
  112. int    argc;
  113. char*    argv[];
  114. {
  115. int    c, i, job, nf;
  116. char*    *fl, *of_name=0;
  117. #ifdef    FITS_IMAGE
  118. extern    int    FTy;
  119. #endif
  120.  
  121. Progname = *argv;
  122. p_use_pound_sign(True);
  123.  
  124.     if ((nf=parse_argus(&fl, argc, argv, arg_fmt, &init_type,
  125.     &pform, &forceF,
  126.     &header, &i_row, &i_cln, &frames,
  127.     &pform, &pform, &pform,
  128.     &pipe, &bufsize, &adj, &filling,
  129.     &enlg_cln, &enlg_row,
  130.     &enlg_cln, &enlg_row,    /* + */
  131.     &gray_scale, &of_name,
  132.     &relocat, &bgn_row, &bgn_cln, &frm,
  133.     &szchange, &o_row, &o_cln, &o_frm,
  134.     &sliding,
  135. #ifdef    _DEBUG_
  136.     &debug,
  137. #endif
  138. #ifdef    FITS_IMAGE
  139.     &FTy,
  140. #endif
  141.     &verbose, &graph,
  142.     &job, &offs, &offs, &offs)) < 0)    exit(-1);
  143.     else    {
  144.     if (of_name && !(out_fp=freopen(of_name, "wb", stdout)))
  145.             syserr("open output file %s\n", of_name);
  146.  
  147.     if (nf)    strcpy(i_name, uimg.name = fl[0]);
  148.     }
  149. if (!strlen(i_name))
  150. #ifdef    TC_Need
  151. {    mesg("input original file name ");    gets(i_name);    }
  152. #else
  153. ;else
  154. #endif
  155. if (!(in_fp=freopen(i_name, "rb", stdin)))
  156.     syserr("input file error\n");
  157.  
  158. uimg.color_dpy = -(!gray_scale);    /* for color HIPS    */
  159. format_init(&uimg, init_type, HIPS, -1, *argv, "S20-1");
  160. io_test(fileno(in_fp), {parse_usage(arg_fmt); exit(0);});
  161.  
  162. fseek(in_fp, 0L, 0);    /*    if piped, errno = 29    */
  163. pipe += errno;        /*    set piping-in flag    */
  164.  
  165. if (header)    {    /* 'p' count spelane color frame as 3 frames    */
  166.     if ((*uimg.header_handle)(HEADER_READ, &uimg, 'p', 0, Yes) < 0)
  167.         syserr("Unknown image type");
  168.     frames= uimg.frames;
  169.     i_cln = uimg.width;    i_row = uimg.height;
  170.     if (pform==IFMT_HIST) {
  171.     MType    v[4];
  172.         fread(v, sizeof(v[0]), 2, in_fp);
  173.         if (!adj)
  174.             fprintf(out_fp, "%s %4d %4d    %d %d %d\n", HIST_TITLE_STR,
  175.             v[0], v[1], uimg.height, uimg.width, uimg.frames);
  176.         for (c=0, o_row=v[1]>>2; c<frames; c++)    {
  177.             fread(&offs, sizeof(offs), 1, in_fp);
  178.             if (!adj)
  179.                 fprintf(out_fp, "maxcnt %d\n", offs);
  180.             for (i=0; i<o_row; i++){
  181.             register int    n = i<<2;
  182.             fread(v, sizeof(v[0]), 4, in_fp);
  183.             if (adj) {
  184.                 fprintf(out_fp, "%d %d\n%d %d\n%d %d\n%d %d\n",
  185.                 n, v[0], n+1, v[1], n+2, v[2], n+3, v[3]);
  186.             }
  187.             else fprintf(out_fp, "%08x %9d %9d %9d %9d\n",
  188.                 n, v[0], v[1], v[2], v[3]);
  189.             }
  190.         }
  191.         exit(0);
  192.     }
  193. }
  194. else{    /*    uimg.frames = frames;    only for loading file */
  195.     uimg.width = i_cln;
  196.     uimg.height = i_row;
  197.     if (pform==IFMT_BYTE || pform==IFMT_ILC)
  198.         uimg.pxl_in = sizeof(char);
  199.     else if (pform==IFMT_SHORT)
  200.         uimg.pxl_in = sizeof(short);
  201.     else    uimg.pxl_in = sizeof(float);
  202.     uimg.channels = uimg.color_dpy ? 3 : 1;
  203.     (*uimg.std_swif)(FI_INIT_NAME, &uimg, i_name, 0);
  204. }
  205. uimg.pxl_out = uimg.pxl_in;
  206. pxlog_bytes = uimg.pxl_out>>1;
  207. if (!gray_scale | !header)
  208.     uimg.o_form = uimg.in_form,
  209.     uimg.dpy_channels = uimg.channels;    /* for line 330 only    */
  210.  
  211. if (enlg_cln < 1)
  212.     if (enlg_row > 1)
  213.         enlg_cln = enlg_row;
  214.     else    enlg_cln = 1;
  215. if (enlg_row < 1)    enlg_row = 1;
  216.  
  217. if (relocat)    {
  218.     showin_info;
  219.     getvsize("resize area begin at [row column [frame]] ",
  220.         &bgn_row, &bgn_cln, &frm);
  221. }
  222.  
  223. if (szchange)    {
  224.     if (!o_row)    {
  225.  rd:    showin_info;
  226.     getvsize("The new window size is [row column [frame(s)]] ",
  227.         &o_row, &o_cln, &o_frm);
  228.     }
  229.     else if(!o_cln)    o_cln = i_row * o_cln / i_row;
  230.     if(bgn_cln + o_cln > i_cln && o_cln % i_cln)    goto    rd;
  231. }
  232. else    o_row = i_row,    o_cln = i_cln;
  233.  
  234. sprintf(cmd,"%s: e%d*%d_%d_%d", i_name, enlg_row, enlg_cln, bgn_row,bgn_cln);
  235. (*uimg.header_handle)(ADD_DESC, &uimg, cmd);
  236.  
  237. if (o_cln * enlg_cln > bufsize)    /* maximum buffer size    */
  238.     o_cln = bufsize / enlg_cln;
  239.  
  240. c = ratio = o_cln / i_cln;    /* c is boolean */
  241. mulf_rowofs = i_row - o_row;
  242. uimg.sub_img_h = o_row * enlg_row;
  243. uimg.sub_img_w = o_cln * enlg_cln;
  244. uimg.sub_img = True;
  245.  
  246. if (adj)    /* get deviation    */
  247. {    adj = uimg.width - o_cln * enlg_cln;
  248.     msg("adjust margin = %d\n", adj);
  249. }
  250. if (frm<1 || frm>frames)    frm = 1;
  251. frames -= frm-1;
  252. in_size = i_cln * (frames * i_row - (bgn_row-1)) - (bgn_cln-1);
  253. wk_sz = o_row * o_cln;
  254.  
  255. /*    Set up output frames:
  256.     when output columns > input columns & output frames < 1
  257.     | output frames > total input pixel : total output pixel
  258. */
  259. if (c && o_frm<1 || o_frm>in_size/wk_sz){
  260.     o_frm = in_size/wk_sz;
  261.     o_frm += o_frm*wk_sz != in_size;
  262. }
  263. else if (o_frm<1 || o_frm>frames)    {
  264.     o_frm = frames;
  265.     if (o_row>i_row)
  266.         if    (filling || o_frm==1)
  267.         orw = o_row - i_row;
  268.         else    o_frm *= (float)i_row/o_row;
  269. }
  270. if (forceF)    o_frm = forceF;
  271. job |=  o_cln != i_cln || o_row != i_row ||
  272.     bgn_row>1 || bgn_cln>1 || enlg_row>1 || enlg_cln>1;
  273. uimg.load_all = uimg.frames;    /* save orig frames for loading file */
  274. uimg.frames = o_frm;
  275. if (job && uimg.in_color==CFM_SEPLANE)    o_frm *= uimg.dpy_channels;
  276. wk_sz *= o_frm;
  277. o_size = uimg.sub_img_h * uimg.sub_img_w * o_frm;
  278.  
  279. if (job & 1)    pw_read(in_size);
  280. topbd = (char*)inbuf + (in_size << pxlog_bytes);
  281.  
  282. /* don't update again while HEADER_WRITE */
  283. uimg.update_header = (*uimg.header_handle)(HEADER_TO, &uimg, Yes, No);
  284. (*uimg.header_handle)(graph ? HEADER_WRITE : HEADER_FWRITE, &uimg,
  285.     argc, argv, stderr);
  286.  
  287. #ifdef    _DEBUG_
  288. if(verbose)
  289.    message("INBUF = %u, r_e=%ld, c_e=%ld, pxlog_bytes=%ld\n",
  290.     inbuf, i_row-bgn_row, i_cln-bgn_cln, pxlog_bytes);
  291. #endif
  292.  
  293. /********************************
  294. *    BEGIN Enlarge        *
  295. * Set column & row deviation    *
  296. ********************************/
  297.  
  298. if (job & 1)    {
  299.     obuf = zalloc(bufsize, 1<<pxlog_bytes, "obuf");
  300.     if (pipe)
  301.     if (pform==IFMT_SHORT)    i = pspowertool(inbuf, obuf, c, orw);
  302.     else if (pform&IFMT_FLOAT)    i = pfpowertool(inbuf, obuf, c, orw);
  303.     else    i = ppowertool(inbuf, obuf, c, orw);
  304.     else
  305.     if (pform==IFMT_SHORT)    i = spowertool(obuf, c, orw);
  306.     else if (pform&IFMT_FLOAT)    i = fpowertool(obuf, c, orw);
  307.     else    i = powertool(obuf, c, orw);
  308.     message("\n%s Ok ! line [%d]\n", *argv, i);
  309. }
  310. else    {
  311.     errno = o_size = uimg.load_all = 0;
  312.     c = o_row*o_cln << pxlog_bytes;
  313.     for (i=0; i<o_frm; i++)    {
  314.         pw_read(i ? 0 : c*uimg.dpy_channels);
  315.         o_size += fwrite(inbuf, uimg.dpy_channels, c, out_fp);
  316.         if (job == 'X')    offs = 0;
  317. #ifdef    RLE_IMAGE
  318.         if (uimg.in_type == RLE && forceF)    {
  319.             if (rle_get_setup(&rle_dflt_hdr) != RLE_SUCCESS ||
  320.             rle_dflt_hdr.xmax - rle_dflt_hdr.xmin + 1
  321.             != uimg.width || uimg.height !=
  322.             rle_dflt_hdr.ymax - rle_dflt_hdr.ymin + 1)
  323.             break;
  324.         }
  325. #endif
  326.     }
  327.     prgmerr(0, "%s [%d]", o_size==c*o_frm ? "convert" : "failure", o_size);
  328. }
  329. fclose(in_fp);    fclose(out_fp);
  330. }
  331.  
  332. bseek(p, len, dir)    /* buffer seek    */
  333. char**    p;
  334. MType    len;
  335. {
  336. switch (dir) {
  337.     case 0:    *p = (char*)len;    break;
  338.     case 1:    *p += len;        break;
  339.     case 2:    *p = topbd + len;
  340. }
  341. return    (*p < inbuf || *p >= topbd);
  342. }
  343.  
  344. WriteLn(r)
  345. {
  346. register int    j, l, wl;
  347. for (j=l=0; j < enlg_row; j++)    {
  348.     wl = fwrite(obuf, 1 << pxlog_bytes, enlg_cln * o_cln, out_fp);
  349.     if (wl != (enlg_cln * o_cln))
  350.         syserr("WriteLn %d", r);
  351.     l += wl;
  352.     if (adj)    l += fwrite(obuf, 1 << pxlog_bytes, adj, out_fp);
  353. }
  354. return    l;
  355. }
  356.  
  357. fill(size, ch)
  358. register char    ch;
  359. {
  360. register char*    p=obuf;
  361. register int    i, l, s;
  362. if (size < 1)    return    0;
  363.     for (i=MIN(bufsize, size)<<pxlog_bytes; i;)
  364.         p[--i] = ch;
  365.     l = size / bufsize;
  366.     s = size - bufsize * l;
  367.     while (l--)
  368.         i += fwrite(p, 1<<pxlog_bytes, bufsize, out_fp);
  369.     i += fwrite(p, 1<<pxlog_bytes, s, out_fp);
  370.     if (i != size)    syserr("filling %d [%d]", i, size);
  371.     else if (verbose)
  372.         message("filling %5d value %d    ", i, ch);
  373. return    i;
  374. }
  375.  
  376. void    say(len, f)
  377. {
  378.     if (sliding)    mesg("\r");    else    mesg("\n");
  379.     message("strip at frame %4d (%d frames [f%2d] => %d)    ",
  380.         frm, ratio, uimg.frames-f+1, len);
  381.     frm += ratio;
  382. }
  383.  
  384.  
  385. /*=======================================
  386. %    main procedure    POWERTOOL    %
  387. =======================================*/
  388. #define    main_pro_POWERTOOL(p_type, inp, shifts)    \
  389. register int    c, r,    /*    control variables    */    \
  390. /*    start not at pos 1, 1;    x_err used as n - 1.    */    \
  391.     c_err = i_cln - bgn_cln,    \
  392.     r_err = i_row - bgn_row;    \
  393. \
  394. for (; o_frm > 0; o_frm--)    {    \
  395.     if (orw)                \
  396.     wlen += fill((orw>>1)*o_cln*enlg_cln, filling);    \
  397.     for (r=0; r < o_row-orw; r++)    {        \
  398.     register p_type    *obp=olinebuf;            \
  399.     Fpos = (MType)(int)inp;                \
  400.     for (c=0; c < o_cln; c++, inp++)    {    \
  401.     register int    j=enlg_cln;            \
  402.         while(j--)    /* duplicate (enlarge) column    */    \
  403.         *obp++ = *inp;                \
  404.         /*    EOL in o_cln    */        \
  405.         if (oc && !((c - c_err) % i_cln)){    /* for spread only */    \
  406.         if (filling && bgn_cln>1){        \
  407.         register MType    i;    /* fill Big Column = Old Frame width */    \
  408.             for (i=bgn_cln; --i;)        \
  409.                 for (c++, j=enlg_cln; j--;)    \
  410.                 *obp++ = filling;    \
  411.             bseek(&inp, (bgn_cln-1) << shifts, 1);    \
  412.         }                    \
  413.         if (bseek(&inp, i_cln * (i_row - 1) << shifts, 1))    {    \
  414. /*    #ifdef    _DEBUG_    \
  415.             if(debug)    msg("inp=%d, r=%d, c=%d, ws=%ld, is=%ld\n",    \
  416.                 inp, r+1, c+1, wlen, in_size);    \
  417.     #endif    */    \
  418.             if (!((r - ((r_err+1) & -!!filling)) % i_row) &&    \
  419.             (filling || wlen >= in_size))    \
  420.             goto    finish;            \
  421.             bseek(&inp, Fpos + (i_cln << shifts), 0);    \
  422.             wlen += WriteLn(r);            \
  423.             goto    nextrow;        \
  424.         }    \
  425.         }        \
  426.     }        \
  427.     wlen += WriteLn(r);    \
  428.     if (oc)            \
  429.         if ((r - r_err) % i_row)    /* not EOFrame    */    \
  430.         bseek (&inp, (-o_cln * i_row + i_cln) << shifts, 1);    \
  431.         else    {                    \
  432.         if (verbose)    say(wlen, o_frm);        \
  433.         if (filling && bgn_row>1)    {        \
  434.             wlen += fill((bgn_row-1)*o_cln*enlg_cln, filling);    \
  435.             r += bgn_row - 1;            \
  436.             bseek(&inp, -r_err*i_cln << shifts, 1);        \
  437.         }    \
  438.         else    bseek(&inp, i_cln*(1-i_row) << shifts, 1);    \
  439.         }    \
  440.     else    bseek(&inp, i_cln - o_cln << shifts, 1);            \
  441. \
  442.     nextrow:    continue;    \
  443.    }    \
  444.    if (mulf_rowofs>0)    bseek(&inp, mulf_rowofs*i_cln << shifts, 1);    \
  445. }    \
  446. finish:    fill(o_size-wlen, FILLER);    return    r+orw;
  447.  
  448.  
  449. ppowertool(ibp, olinebuf, oc, orw)
  450. char*    ibp, *olinebuf;
  451. register int    oc;    /*    spread : o_cln > i_cln    */
  452. {
  453. main_pro_POWERTOOL(char, ibp, 0);
  454. }
  455.  
  456. pspowertool(isp, olinebuf, oc, orw)/*    for short    */
  457. short    *isp, *olinebuf;
  458. register int    oc;
  459. {
  460. main_pro_POWERTOOL(short, isp, pxlog_bytes);
  461. }
  462.  
  463. pfpowertool(ifp, olinebuf, oc, orw)/*    for all four bytes    */
  464. long    *ifp, *olinebuf;
  465. register int    oc;
  466. {
  467. main_pro_POWERTOOL(long, ifp, pxlog_bytes);
  468. }
  469.  
  470.     /*=======================================
  471.     %    for non piped input mode    %
  472.     %    with less main memory available    %
  473.     =======================================*/
  474. #define    non_pipe_POWERTOOL(p_type, func, shifts)    \
  475. register int    c, j, r,    /*    control variables    */    \
  476. /*    start not at pos 0,0    */    \
  477.     c_err = i_cln - bgn_cln,    \
  478.     r_err = i_row - bgn_row;    \
  479. MType    NewFpos, FF = ftell(in_fp);    \
  480.     \
  481. for (; o_frm > 0; o_frm--)    {    \
  482.     if (orw)                \
  483.     wlen += fill((orw>>1)*o_cln*enlg_cln<<shifts, filling);    \
  484.     for (r=0; r < o_row-orw; r++)    {    \
  485.     Fpos = ftell(in_fp);            \
  486.     for (c=0; c < o_cln; c++)    {    \
  487.     p_type    *obp=olinebuf + c*enlg_cln;    \
  488.         {    /* duplicate (enlarge) column    */\
  489.         p_type    b;    func;        \
  490.         for (j=0; j < enlg_cln; j++)    \
  491.             obp[j] = b;        \
  492.         }    \
  493.         /*    o_c > i_c and EOL, goto next frame same line    */\
  494.         if (oc && !((c - c_err) % i_cln)){    \
  495.         if (filling && bgn_cln>1){    \
  496.         register MType    i;    /* fill Big Column = Old Frame width */    \
  497.             for (i=0; i<bgn_cln-1; i++)        \
  498.                 for (c++, obp+=enlg_cln, j=0; j<enlg_cln; j++)    \
  499.                 obp[j] = filling;        \
  500.             fseek(in_fp, i << shifts, 1);    \
  501.         }    \
  502.         if (fseek(in_fp, i_cln * (i_row - 1) << shifts, 1))    \
  503.             syserr("fsk=%d, r=%d, c=%d\n", errno, r, c);        \
  504.         NewFpos = ftell(in_fp);        \
  505.         /* no more frame and also this is last frame last row */    \
  506.         if (NewFpos > FF + in_size){    \
  507.             if (!((r - ((r_err+1) & -!!filling)) % i_row) &&    \
  508.             (filling || wlen >= in_size))            \
  509.                 goto    finish;                \
  510.             fseek(in_fp, Fpos + (i_cln << shifts), 0);    \
  511.             wlen += WriteLn(r);    \
  512.             goto    nextrow;    \
  513.         }    \
  514.         }        \
  515.     }        \
  516.     wlen += WriteLn(r);    /* duplicate (enlarge) row    */    \
  517.     if (oc)    /* o_c > i_c without enlarge    */            \
  518.        if ((r - r_err) % i_row) /* not EOFrame, back n frames next row */    \
  519.         fseek(in_fp, ((-o_cln * i_row) + i_cln) << shifts, 1);    \
  520.        else    {    /* EOFrm, already at br-bc of next frame    */    \
  521.         if (verbose)    say(wlen, o_frm);    \
  522.         if (filling && bgn_row>1){        \
  523.             wlen += fill((bgn_row-1)*o_cln*enlg_cln, filling);    \
  524.             r += bgn_row - 1;        \
  525.             fseek(in_fp, -r_err*i_cln << shifts, 1);    \
  526.         }    \
  527.         else    fseek(in_fp, i_cln*(1-i_row) << shifts, 1);    \
  528.         }    \
  529.     else    /* in regular, goto next line (row) */        \
  530.         fseek(in_fp, (i_cln - o_cln) << shifts, 1);\
  531.     nextrow:    continue;    \
  532.     }    \
  533.     if (mulf_rowofs>0)    fseek(in_fp, mulf_rowofs*i_cln<<shifts, 1);    \
  534. }    \
  535. finish:    fill(o_size-wlen, FILLER);    return    r+orw;
  536.  
  537.  
  538. powertool(olinebuf, oc, orw)
  539. char    *olinebuf;
  540. register int    oc;    /*    o_cln > i_cln    */
  541. {
  542.     non_pipe_POWERTOOL(register char, b=fgetc(in_fp) & 0xFF, 0);
  543. }
  544.  
  545. spowertool(olinebuf, oc, orw)
  546. register short*    olinebuf;
  547. register int    oc;
  548. {
  549.     non_pipe_POWERTOOL(short, fread(&b, 1, sizeof(b), in_fp), 0);
  550. }
  551.  
  552. fpowertool(olinebuf, oc, orw)
  553. long    *olinebuf;
  554. register int    oc;
  555. {
  556.     non_pipe_POWERTOOL(register long, b=getw(in_fp), pxlog_bytes);
  557. }
  558.  
  559. /*    routine for reading window size when resize or scale error    */
  560. #ifndef    TC_Need
  561. getvsize(s,a,b,c)
  562. char*    s;
  563. int*    a,*b,*c;
  564. {
  565.     mesg(s);    read(2, cmd, sizeof(cmd));
  566.     return    sscanf(cmd,"%d %d %d",a,b,c);
  567. }
  568. #else
  569. getvsize(char* s, ...)
  570. {
  571.     mesg(s);    read(2, cmd,sizeof(cmd));
  572.     return    vsscanf(cmd, "%d %d %d", ...);
  573. }
  574. #endif
  575.  
  576. pw_read(b_size)
  577. {
  578. /*    pass first begining rows and N frames. See top for note    */
  579. static    int    pb_size;
  580. if (pipe) {
  581.     if (b_size)    pb_size = b_size,
  582.         inbuf = nzalloc(b_size*uimg.channels, 1<<pxlog_bytes, "inbuf");
  583.     if (offs)    upread(inbuf, offs, 1, in_fp);
  584.     if (b_size = (((frm-1) * i_row + (bgn_row-1)) * i_cln + (bgn_cln-1))
  585.         * uimg.channels)    {
  586.         while (b_size > pb_size)
  587.             b_size -= upread(inbuf, 1<<pxlog_bytes, pb_size, in_fp);
  588.         upread(inbuf, b_size, 1<<pxlog_bytes, in_fp);
  589.     }
  590. return    (*uimg.std_swif)(FI_LOAD_FILE, &uimg, No, uimg.color_dpy);
  591. }
  592. else    {
  593.     msg("%s in file mode\n", Progname);
  594.     fseek(in_fp, offs, 1);
  595.     b_size = (((frm-1) * i_row + (bgn_row-1)) * i_cln + (bgn_cln-1))
  596.         * uimg.channels;
  597.     if (fseek(in_fp, b_size, 1))
  598.         syserr("passing 2 (%d)", b_size);
  599. }
  600. }
  601.